package com.example.starapp.starfrag;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.animee.starapp.starfrag.StarInfo;
import com.example.starapp.R;
import com.example.starapp.utils.AssetsUtils;

import java.util.List;

public class StarBaseAdapter extends BaseAdapter {
    Context context;
    List<StarInfo.StarinfoBean> mDatas;

    public StarBaseAdapter(Context context, List<StarInfo.StarinfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_star_gv,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else {
            holder= (ViewHolder) convertView.getTag();
        }
        StarInfo.StarinfoBean bean = mDatas.get(position);
        holder.tv.setText(bean.getName()); //获取到星座名，设置到指定的TextView上
        String logoname = bean.getLogoname();
//        将图片所在的文件的整体路径名设置好
        String filename = "xzlogo/"+logoname+".png";
        Bitmap bitmap = AssetsUtils.getBitmapByAssets(context, filename); //将图片转换成bitmap
//        将bitmap设置给imageview
        holder.iv.setImageBitmap(bitmap);
        return convertView;
    }
    class ViewHolder{
        ImageView iv;
        TextView tv;
        public ViewHolder(View view){
            iv=view.findViewById(R.id.item_star_iv);
            tv=view.findViewById(R.id.item_star_tv);
        }
    }
}